package com.yunsc.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.media.entity.YunscUploadFileEntity;
import com.yunsc.media.service.YunscUploadFileService;
import com.yunsc.product.dao.YunscGoodsImageDao;
import com.yunsc.product.dao.YunscGoodsSkuDao;
import com.yunsc.product.dto.GoodsSkuDTO;
import com.yunsc.product.entity.YunscGoodsSkuEntity;
import com.yunsc.product.service.YunscGoodsSkuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.yunsc.media.utils.FormatUploadFile.formatFile;


@Service("yunscGoodsSkuService")
public class YunscGoodsSkuServiceImpl extends ServiceImpl<YunscGoodsSkuDao, YunscGoodsSkuEntity> implements YunscGoodsSkuService {


    @Resource
    private YunscUploadFileService uploadFileService;
    @Resource
    private YunscGoodsImageDao goodsImageDao;

    @Override
    public List<GoodsSkuDTO> querySkuListByGoodsId(Integer goodsId) {
        List<GoodsSkuDTO> goodsSkuDTOS = this.baseMapper.selectByGoodsId(goodsId);
        if (CollectionUtils.isEmpty(goodsSkuDTOS)) {
            return Collections.emptyList();
        }
        goodsSkuDTOS.forEach(sku -> {
                    // 设置商品图片
                    if (sku.getImage() != null && sku.getImageId() > 0) {
                        sku.setImage(setGoodsSkuImage(sku));
                        sku.setImageUrl(uploadFileService.queryFileUrlById(sku.getImageId()));
                    }
                    // 设置商品属性
                    Object goodsProps = sku.getGoodsProps();
                    if (goodsProps != null) {
                        List<Map<String, Object>> list = JSON.parseObject(goodsProps.toString(), List.class);
                        sku.setGoodsProps(list);
                        // List<Map<String, Object>> props = getGoodsProps(goodsProps.toString());

                        // sku.setGoodsProps(props);
                    }
                    // 设置规格值ID集
                    Object specValueIds = sku.getSpecValueIds();
                    if (specValueIds != null) {
                        List<String> list = JSON.parseArray(specValueIds.toString(), String.class);
                        sku.setSpecValueIds(list);
                    }

                }
        );
        return goodsSkuDTOS;
    }

    @Override
    public int editGoodsSkuStockNum(Integer goodsId, String goodsSkuId, Integer stockNum) {
        return this.baseMapper.reduceGoodsSkuStockNum(goodsId, goodsSkuId, stockNum);
    }

    @Override
    public void addGoodsSkuStockNum(Integer goodsId, String goodsSkuId, Integer totalNum) {
        this.baseMapper.increaseGoodsSkuStockNum(goodsId, goodsSkuId, totalNum);
    }

    private YunscUploadFileEntity setGoodsSkuImage(GoodsSkuDTO sku) {
        YunscUploadFileEntity image = sku.getImage();
        if (image == null) {
            return null;
        }
        formatFile(image);
        return image;
    }

    public List<Map<String, Object>> getGoodsProps(String props) {
        // 解析JSON数组到List<Map<String, Object>>
        JSONArray jsonArray = JSONArray.parseArray(props);
        if (StringUtils.isEmpty(props)){
            return Collections.emptyList();
        }
        List<Map<String, Object>> specItemsAsMaps = new ArrayList<>();

        for (Object obj : jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            Map<String, Object> itemMap = jsonObject.toJavaObject(Map.class);
            // 将嵌套的对象也转为Map
            Map<String, Object> groupMap = ((JSONObject) itemMap.get("group")).toJavaObject(Map.class);
            Map<String, Object> valueMap = ((JSONObject) itemMap.get("value")).toJavaObject(Map.class);

            // 将group和value替换为它们对应的Map对象
            itemMap.put("group", groupMap);
            itemMap.put("value", valueMap);

            specItemsAsMaps.add(itemMap);
        }

        return specItemsAsMaps;
    }



}